package com.globagames.strategicgames.command
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.globagames.isometric.core.business.FileAccessDelegate;
	import com.globagames.isometric.core.model.Block;
	import com.globagames.isometric.core.model.IMapModule;
	import com.globagames.isometric.core.model.IsoTile;
	import com.globagames.isometric.core.model.SimpleObject;
	import com.globagames.strategicgames.event.CreateWorldMapEvent;
	
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	
	import mx.rpc.IResponder;

	public class CreateWorldMapCommand implements Command, IResponder
	{
		private var delegate : FileAccessDelegate;
		private var e:CreateWorldMapEvent;
		private var currentTask:String;
		public function CreateWorldMapCommand()
		{
			
		}

		public function execute(event:CairngormEvent):void
		{		
			e = event as CreateWorldMapEvent;
			e.flagsSimpleObjects = new Array();
			var map:IMapModule = e.mapModule;	
			map.tilesTable = new Array(map.sizeY);
				trace("_tilesTable.size="+map.tilesTable.length);
				for(var iy:int=0 ; iy < map.sizeY; iy++){	
					map.tilesTable[iy] = new Array(map.sizeX);				
					for (var ix:int=0;ix<map.sizeX;ix++){	
						var t:IsoTile = new IsoTile();
						t.x2d = ix;
						t.y2d = iy;
						t.x3d = ix*32 - iy*32;
						t.y3d = ( ix*32 + iy*32 )/2;	
						t.design.push(0);
						map.tilesTable[iy][ix] = t;
					}
				}	
						
				map.bitmapsRects = new Array();
				for ( var yi:int = 0 ; yi < 40 ; yi++ ) {
					for ( var xi:int = 0 ; xi < 40 ; xi++ ) {
						var xpos:int = xi * 64;
						var ypos:int = yi * 32;
						var b:Block = new Block();
						b.bitmapNumber=0;
						b.rect =  new Rectangle(xpos,ypos,64,32);
						b.type = Block.TYPE_SOL;
						map.bitmapsRects .push(b);						
					}
				}
				var mxi :int= 1;
				var myi:int =40;
				for ( var yi:int = 0 ; yi < 40 ; yi++ ) {
					for ( var xi:int = 0 ; xi < 40 ; xi++ ) {
						var xtile:int = mxi + xi;
						var ytile:int = myi - xi;
						
						var tile:IsoTile = map.tilesTable[ytile][xtile] as IsoTile; 						
						tile.design.pop();
						tile.design.push((yi * 40 + xi +1));
					}
					for ( var xi:int = 0 ; xi < 40 ; xi++ ) {
						var xtile:int = mxi + xi - 1;
						var ytile:int = myi - xi;
						var tile:IsoTile = map.tilesTable[ytile][xtile] as IsoTile; 
						tile.design.pop();
						tile.design.push(-1);
					}
					myi++;
					mxi++;
				}	
				delegate = new FileAccessDelegate( this );				
				currentTask = "load_bitmap_griffons";
				delegate.loadPNGFile("assets/flag-griffons.png",false);
				
		}
		public function result( event : Object ) : void
		{	
			if ( currentTask == ("load_bitmap_griffons") ) {
				var bitmapData:BitmapData = event as BitmapData;
				currentTask = "load_bitmap_wolfen";
				var so:SimpleObject = new SimpleObject();
				so.bitmapData = bitmapData;
				e.flagsSimpleObjects.push(so);
				delegate.loadPNGFile("assets/flag-wolfen.png",false);
					
			} else if ( currentTask == ("load_bitmap_wolfen") ) {
				var bitmapData:BitmapData = event as BitmapData;
				var so:SimpleObject = new SimpleObject();
				so.bitmapData = bitmapData;
				e.flagsSimpleObjects.push(so);
				e.mapLoaded = true;
			}
			
		}
		
		public function fault( event : Object ) : void
		{			
			trace(event.fault.rootCause.message, event.fault.faultCode);
		}
		
	}
}